﻿

using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System;

namespace 采集卡配置
{
    class ini
    {
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string mpAppName, string mpKeyName, string  mpDefault, string mpFileName);

/// <summary>
        /// 读取INI文件值
         /// </summary>
         /// <param name="section">节点名</param>
        /// <param name="key">键</param>
        /// <param name="def">未取到值时返回的默认值</param>
        /// <param name="filePath">INI文件完整路径</param>
         /// <returns>读取的值</returns>
         public static string ini_Read(string section, string key, string def, string filePath)
        {
            StringBuilder sb = new StringBuilder(1024);
             GetPrivateProfileString(section, key, def, sb, 1024, filePath);
            return sb.ToString();
         }
 
        /// <summary>
        /// 写INI文件值
         /// </summary>
        /// <param name="section">欲在其中写入的节点名称</param>
         /// <param name="key">欲设置的项名</param>
         /// <param name="value">要写入的新字符串</param>
         /// <returns>非零表示成功，零表示失败</returns>
        public static long ini_Write(string section, string key, string  value, string filePath)
         {
            if (File.Exists(filePath))
                return WritePrivateProfileString(section, key, value, filePath);
            else return 0;
        }

        /// <summary>
         /// 删除节
         /// </summary>
         /// <param name="section">节点名</param>
         /// <param name="filePath">INI文件完整路径</param>
        /// <returns>非零表示成功，零表示失败</returns>
         public static long DeleteSection(string section, string filePath)
         {
             return ini_Write(section, null, null, filePath);
         }
 
         /// <summary>
         /// 删除键的值
         /// </summary>
         /// <param name="section">节点名</param>
         /// <param name="key">键名</param>
         /// <param name="filePath">INI文件完整路径</param>
         /// <returns>非零表示成功，零表示失败</returns>
         public static long DeleteKey(string section, string key, string filePath)
         {
             return ini_Write(section, key, null, filePath);
         }
        private static string ContentReader(string area, string key, string def, string str)                       //用来读取INI文件的方法
        {
            StringBuilder stringBuilder = new StringBuilder(1024);                            //定义一个最大长度为1024的可变字符串
            GetPrivateProfileString(area, key, def, stringBuilder, 1024, str);                      //读取INI文件
            return stringBuilder.ToString();                                               //返回INI文件的内容
        }
       
        public static string get_inifile(string path_str)
        {
            string str = "";                                                                    //该变量保存INI文件所在的具体物理位置
            string strOne = "";
            //  str = Application.StartupPath + "\\config\\userinit.ini";                                      //INI文件的物理地址
            //此方法也可通过：str = System.AppDomain.CurrentDomain.BaseDirectory + @"ConnectString.ini";
            strOne = System.IO.Path.GetFileNameWithoutExtension(path_str);                        //获取INI文件的文件名
            strOne = "SysSetup";
            if (File.Exists(path_str))                                                                //判断是否存在该INI文件
            {
                return ContentReader(strOne, "n_Sample ", "", path_str);                         //读取INI文件中服务器节点的内容
                                                                                                   //  textBox18.Text = ContentReader(strOne, "DataBase", "");                          //读取INI文件中数据库节点的内容
                                                                                                   //   textBox15.Text = ContentReader(strOne, "Uid", "");                                          //读取INI文件中用户节点的内容
                                                                                                   //  textBox17.Text = ContentReader(strOne, "Pwd", "");                                      //读取INI文件中密码节点的内容
                                                                                                   //   textBox15.Text = ContentReader(strOne, "Ip", "");                                          //读取INI文件中用户节点的内容
                                                                                                   //   textBox17.Text = ContentReader(strOne, "Port", "");                                      //读取INI文件中密码节点的内容
            }
            return null;


        }


        public static string get_ini_val(string section,  string key)
        {
                                                                             //该变量保存INI文件所在的具体物理位置
         //   string strOne = "";
            string path_str = System.Windows.Forms.Application.StartupPath + "\\config.ini";
   
            if (File.Exists(path_str))                                                                //判断是否存在该INI文件
            {
                return ContentReader(section, key, "", path_str);                         //读取INI文件中节点的内容                                                                                                 
            }
            return null;
        }

        public static long set_ini_val(string section,string area, string  key)
        {
            //该变量保存INI文件所在的具体物理位置
            //   string strOne = "";
            string path_str = System.Windows.Forms.Application.StartupPath + "\\config.ini";

            if (File.Exists(path_str))                                                                //判断是否存在该INI文件
            {
                return ini_Write(section,area, key, path_str);                         //读取INI文件中节点的内容                                                                                                 
            }
            return 0;
        }













        void set_inifile(string str)
        {
            // test();
            if (File.Exists(str))                                                                 //判断是否存在INI文件
            {
                //  WritePrivateProfileString(strOne, "Data Source", textBox_IP.Text, str);          //修改INI文件中服务器节点的内容
                //  WritePrivateProfileString(strOne, "DataBase", textBox18.Text, str);           //修改INI文件中数据库节点的内容
                //  WritePrivateProfileString(strOne, "Uid", textBox15.Text, str);                    //修改INI文件中用户节点的内容
                //  WritePrivateProfileString(strOne, "Pwd", textBox17.Text, str);                 //修改INI文件中密码节点的内容
                //  WritePrivateProfileString(strOne, "Ip", textBox17.Text, str);                 //修改INI文件中IP节点的内容
                //  WritePrivateProfileString(strOne, "Port", textBox17.Text, str);                 //修改INI文件中port节点的内容
                //  MessageBox.Show("恭喜你，修改成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                ;// MessageBox.Show("对不起，你所要修改的文件不存在，请确认后再进行修改操作！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
    class ch_config
    {
        public byte[] Range = new byte[8];  //增益
        public byte[] Coupling;//ao合
        public UInt32 Sample;
        public Double sample_time;
        public string[] str_unit = new string[4];
        public void get_config()
        {
            string[] row = new string[10];

            string[] section_str = new string[] { "clu_VibInfo1", "clu_VibInfo2", "clu_VibInfo3", "clu_VibInfo4" };
            //  this.Analog_input_DGV.Rows.Clear();

            Sample = Convert.ToUInt32(ini.get_ini_val("SysSetup", "n_Sample"));
            sample_time = Convert.ToDouble(ini.get_ini_val("SysSetup", "n_Sample")); 
            for (int i = 0; i < 4; i++)
            {
                if (ini.get_ini_val(section_str[i], "str_Range").Equals("-1V-1V", StringComparison.OrdinalIgnoreCase) == true)
                    Range[i * 2] = 0X00;
                else Range[i * 2] = 0X01;

                if (ini.get_ini_val(section_str[i], "str_Coupling").Equals("DC", StringComparison.OrdinalIgnoreCase) == true)
                    Range[1 + i * 2] = 0X01;
                else Range[1 + i * 2] = 0X00;
                str_unit[i]= Convert.ToString(ini.get_ini_val(section_str[i], "str_unit")); 
            }
        }
        public bool[] get_chanel_states()
        {

            bool[] sta = new bool[4];
            string[] section_str = new string[] { "clu_VibInfo1", "clu_VibInfo2", "clu_VibInfo3", "clu_VibInfo4" };
            for (int i = 0; i < 4; i++)
            {
                if (ini.get_ini_val(section_str[i], "b_Enable").Equals("True", StringComparison.OrdinalIgnoreCase) == true)
                    sta[i] = true;
                else
                    sta[i] = false;
            }
            return sta;

        }

        public string[] get_chanel_name()
        {
            string[] chanel_name = new string[4];
            string[] section_str = new string[] { "clu_VibInfo1", "clu_VibInfo2", "clu_VibInfo3", "clu_VibInfo4" };
            for (int i = 0; i < 4; i++)
            {
                if (ini.get_ini_val(section_str[i], "str_ChanelName") != null)
                    chanel_name[i] = ini.get_ini_val(section_str[i], "str_ChanelName");
                else
                    chanel_name[i] = "Ain";
            }


            return chanel_name;
        }
    }
}

